package model;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import model.beans.Evento;
import model.beans.TipoConsulta;
import model.dao.EventoDAO;
import model.negocio.TipoConsultaNegocio;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Carol
 */
public class JavaMailApp {
    public JavaMailApp(){};
    
      public boolean enviaEmail(Evento evento) {
            Properties props = new Properties();
            /** Parâmetros de conexão com servidor Gmail */
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");

            Session session = Session.getDefaultInstance(props,
                        new javax.mail.Authenticator() {
                             protected PasswordAuthentication getPasswordAuthentication() 
                             {
                                   return new PasswordAuthentication("scc.consultas@gmail.com", "massoterapia123");
                             }
                        });

            /** Ativa Debug para sessão */
            session.setDebug(true);

            try {

                  Message message = new MimeMessage(session);
                  message.setFrom(new InternetAddress("scc.consultas@gmail.com")); //Remetente

                  EventoDAO dao = new EventoDAO();
                  String email = dao.email(evento.getTipo());
                  
                  if(email.equals("")){
                      return false;
                  }else{
                    Address[] toUser = InternetAddress //Destinatário(s)
                               .parse(email);  

                    message.setRecipients(Message.RecipientType.TO, toUser);
                    message.setSubject(evento.getNome());//Assunto
                    
                    TipoConsultaNegocio tp = new TipoConsultaNegocio();
                    TipoConsulta t = tp.buscar(evento.getTipo());
                    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");  
                    String data = formatter.format(evento.getData());
                    String msg = "Data do Evento: " + data + "\nTipo de Evento: " + t.getNome() + "\n" + evento.getMensagem();
                            
                    message.setText(msg);
                    /**Método para enviar a mensagem criada*/
                    Transport.send(message);
                    return true;
                  }

                  //System.out.println("Feito!!!");

             } catch (Exception e) {
                  throw new RuntimeException(e);
            }
      }
}